<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>AMBHAS: /home/tomer/svn/ambhas/ambhas/gis.py Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AMBHAS
   
   </div>
   
  </td>
  
  
  
   
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
</td>
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('gis_8py.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">gis.py</div>  </div>
</div><!--header-->
<div class="contents">
<a href="gis_8py.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a><a class="code" href="namespaceambhas_1_1gis.html">00001</a> <span class="comment">#! /usr/bin/env python</span>
<a name="l00002"></a>00002 <span class="comment"># -*- coding: utf-8 -*-</span>
<a name="l00003"></a>00003 <span class="stringliteral">&quot;&quot;&quot;</span>
<a name="l00004"></a>00004 <span class="stringliteral">Created on Mon Nov 15 19:54:08 2010</span>
<a name="l00005"></a>00005 <span class="stringliteral"></span>
<a name="l00006"></a>00006 <span class="stringliteral">@author: sat kumar tomer (http://civil.iisc.ernet.in/~satkumar/)</span>
<a name="l00007"></a>00007 <span class="stringliteral"></span>
<a name="l00008"></a>00008 <span class="stringliteral">Functions:</span>
<a name="l00009"></a>00009 <span class="stringliteral">    utm2deg: Calculate utm co-ordinates from Lat, Lon</span>
<a name="l00010"></a>00010 <span class="stringliteral">    deg2utm : Calculate Lat, Lon from UTM</span>
<a name="l00011"></a>00011 <span class="stringliteral">    kabini: </span>
<a name="l00012"></a>00012 <span class="stringliteral">    berambadi: </span>
<a name="l00013"></a>00013 <span class="stringliteral">&quot;&quot;&quot;</span>
<a name="l00014"></a>00014 
<a name="l00015"></a>00015 <span class="comment"># load needed python modules </span>
<a name="l00016"></a>00016 <span class="keyword">from</span> pyproj <span class="keyword">import</span> Proj
<a name="l00017"></a>00017 <span class="keyword">import</span> os
<a name="l00018"></a>00018 <span class="keyword">from</span> subprocess <span class="keyword">import</span> call
<a name="l00019"></a>00019 <span class="keyword">import</span> sys
<a name="l00020"></a>00020 <span class="keyword">import</span> numpy <span class="keyword">as</span> np
<a name="l00021"></a>00021 
<a name="l00022"></a><a class="code" href="namespaceambhas_1_1gis.html#ac08652456f1fda96c33b457ff3e22fcf">00022</a> <span class="keyword">def </span><a class="code" href="namespaceambhas_1_1gis.html#ac08652456f1fda96c33b457ff3e22fcf">utm2image</a>(GT,utm):
<a name="l00023"></a>00023     Xpixel = ((utm[:,0] - GT[0])*GT[5] - (utm[:,1] - GT[3])*GT[2])/(GT[1]*GT[5]-GT[4]*GT[2])
<a name="l00024"></a>00024     Ypixel = ((utm[:,1] - GT[3])*GT[1] - (utm[:,0] - GT[0])*GT[4])/(GT[1]*GT[5]-GT[4]*GT[2])
<a name="l00025"></a>00025     <span class="keywordflow">return</span> (np.round(Xpixel)).astype(<span class="stringliteral">&#39;int&#39;</span>),(np.round(Ypixel)).astype(<span class="stringliteral">&#39;int&#39;</span>)
<a name="l00026"></a>00026 
<a name="l00027"></a><a class="code" href="namespaceambhas_1_1gis.html#a12e5754b539f6ea19f9c63a5e52b3fc5">00027</a> <span class="keyword">def </span><a class="code" href="namespaceambhas_1_1gis.html#a12e5754b539f6ea19f9c63a5e52b3fc5">Geo2Pixel</a>(Xgeo,Ygeo,GT):
<a name="l00028"></a>00028     a1 = GT[1]
<a name="l00029"></a>00029     b1 = GT[2]
<a name="l00030"></a>00030     c1 = Xgeo - GT[0]
<a name="l00031"></a>00031     a2 = GT[4]
<a name="l00032"></a>00032     b2 = GT[5]
<a name="l00033"></a>00033     c2 = Ygeo - GT[3]
<a name="l00034"></a>00034     Xpixel = (b2*c1-b1*c2)/(a1*b2-a2*b1) 
<a name="l00035"></a>00035     Yline = (a2*c1-a1*c2)/(a2*b1-a1*b2) 
<a name="l00036"></a>00036     <span class="keywordflow">return</span> Xpixel, Yline
<a name="l00037"></a>00037 
<a name="l00038"></a><a class="code" href="namespaceambhas_1_1gis.html#a663bfc2ba0de54a4693266ab4d0a5d26">00038</a> <span class="keyword">def </span><a class="code" href="namespaceambhas_1_1gis.html#a663bfc2ba0de54a4693266ab4d0a5d26">Pixel2Geo</a>(Xpixel,Yline,GT):
<a name="l00039"></a>00039     Xgeo = GT[0] + Xpixel*GT[1] + Yline*GT[2]
<a name="l00040"></a>00040     Ygeo = GT[3] + Xpixel*GT[4] + Yline*GT[5]
<a name="l00041"></a>00041     <span class="keywordflow">return</span> Xgeo,Ygeo
<a name="l00042"></a>00042 
<a name="l00043"></a><a class="code" href="namespaceambhas_1_1gis.html#a67f66eabcad5eaba0280f2462ddd690f">00043</a> <span class="keyword">def </span><a class="code" href="namespaceambhas_1_1gis.html#a67f66eabcad5eaba0280f2462ddd690f">SetProjectionBerambadi</a>():
<a name="l00044"></a>00044         <span class="keywordflow">return</span> <span class="stringliteral">&#39;PROJCS[&quot;WGS 84 / UTM zone 43N&quot;,GEOGCS[&quot;WGS 84&quot;,DATUM[&quot;WGS_1984&quot;,SPHEROID[&quot;WGS 84&quot;,6378137,298.257223563,AUTHORITY[&quot;EPSG&quot;,&quot;7030&quot;]],AUTHORITY[&quot;EPSG&quot;,&quot;6326&quot;]],PRIMEM[&quot;Greenwich&quot;,0],UNIT[&quot;degree&quot;,0.0174532925199433],AUTHORITY[&quot;EPSG&quot;,&quot;4326&quot;]],PROJECTION[&quot;Transverse_Mercator&quot;],PARAMETER[&quot;latitude_of_origin&quot;,0],PARAMETER[&quot;central_meridian&quot;,75],PARAMETER[&quot;scale_factor&quot;,0.9996],PARAMETER[&quot;false_easting&quot;,500000],PARAMETER[&quot;false_northing&quot;,0],UNIT[&quot;metre&quot;,1,AUTHORITY[&quot;EPSG&quot;,&quot;9001&quot;]],AUTHORITY[&quot;EPSG&quot;,&quot;32643&quot;]]&#39;</span>
<a name="l00045"></a>00045 
<a name="l00046"></a><a class="code" href="namespaceambhas_1_1gis.html#a644557db2a41acd9f65cfaf35cda5b3f">00046</a> <span class="keyword">def </span><a class="code" href="namespaceambhas_1_1gis.html#a644557db2a41acd9f65cfaf35cda5b3f">utm2deg</a>(x,y,utmzone=43):
<a name="l00047"></a>00047     p = Proj(proj=<span class="stringliteral">&#39;utm&#39;</span>,zone=43,ellps=<span class="stringliteral">&#39;WGS84&#39;</span>)
<a name="l00048"></a>00048     Lon,Lat = p(x, y, inverse=<span class="keyword">True</span>)
<a name="l00049"></a>00049     <span class="keywordflow">return</span> Lon,Lat
<a name="l00050"></a>00050     
<a name="l00051"></a><a class="code" href="namespaceambhas_1_1gis.html#a417b86b230594cf512825e2c0ba33329">00051</a> <span class="keyword">def </span><a class="code" href="namespaceambhas_1_1gis.html#a417b86b230594cf512825e2c0ba33329">deg2utm</a>(Lon,Lat,utmzone=43):
<a name="l00052"></a>00052     p = Proj(proj=<span class="stringliteral">&#39;utm&#39;</span>,zone=utmzone,ellps=<span class="stringliteral">&#39;WGS84&#39;</span>)
<a name="l00053"></a>00053     x,y = p(Lon, Lat)
<a name="l00054"></a>00054     <span class="keywordflow">return</span> x,y
<a name="l00055"></a>00055     
<a name="l00056"></a><a class="code" href="namespaceambhas_1_1gis.html#ae92db89a8e9ed497b5e68e7b7c6e7cfc">00056</a> <span class="keyword">def </span><a class="code" href="namespaceambhas_1_1gis.html#ae92db89a8e9ed497b5e68e7b7c6e7cfc">kabini</a>(Ifile,Ofile):
<a name="l00057"></a>00057     <span class="comment"># define the file names file</span>
<a name="l00058"></a>00058     Temp1 = <span class="stringliteral">&#39;/home/tomer/MODISdata/temp/temp1.tif&#39;</span>
<a name="l00059"></a>00059     Temp2 = <span class="stringliteral">&#39;/home/tomer/MODISdata/temp/temp2.tif&#39;</span>
<a name="l00060"></a>00060             
<a name="l00061"></a>00061     <span class="comment"># convert from geographical co-ordinates to UTM zone 43</span>
<a name="l00062"></a>00062     cmd= <span class="stringliteral">&#39;gdalwarp -r bilinear -t_srs  \&#39;+proj=utm +zone=43 +datum=WGS84\&#39; &#39;</span> + Ifile + <span class="stringliteral">&#39; &#39;</span> +Temp1
<a name="l00063"></a>00063     <span class="keywordflow">try</span>:
<a name="l00064"></a>00064         returncode = call(cmd, shell=<span class="keyword">True</span>)
<a name="l00065"></a>00065         <span class="keywordflow">if</span> returncode:
<a name="l00066"></a>00066             <span class="keywordflow">print</span> <span class="stringliteral">&#39;Failure with returncode&#39;</span>, returncode; sys.exit(1)
<a name="l00067"></a>00067     <span class="keywordflow">except</span> OSError, message:
<a name="l00068"></a>00068         <span class="keywordflow">print</span> <span class="stringliteral">&#39;Execution failed!\n&#39;</span>, message; sys.exit(1)
<a name="l00069"></a>00069 
<a name="l00070"></a>00070     <span class="comment"># cut the area around Kabini Basin (output image will be of size 129 X 102)</span>
<a name="l00071"></a>00071     kab=<span class="stringliteral">&#39;582000 1372000 711000 1270000&#39;</span>
<a name="l00072"></a>00072     cmd = <span class="stringliteral">&#39;gdal_translate -a_ullr &#39;</span> + kab + <span class="stringliteral">&#39; -projwin &#39;</span> + kab + <span class="stringliteral">&#39; &#39;</span> + Temp1+ <span class="stringliteral">&#39; &#39;</span> + Temp2
<a name="l00073"></a>00073     <span class="keywordflow">try</span>:
<a name="l00074"></a>00074         returncode = call(cmd, shell=<span class="keyword">True</span>)
<a name="l00075"></a>00075         <span class="keywordflow">if</span> returncode:
<a name="l00076"></a>00076             <span class="keywordflow">print</span> <span class="stringliteral">&#39;Failure with returncode&#39;</span>, returncode; sys.exit(1)
<a name="l00077"></a>00077     <span class="keywordflow">except</span> OSError, message:
<a name="l00078"></a>00078         <span class="keywordflow">print</span> <span class="stringliteral">&#39;Execution failed!\n&#39;</span>, message; sys.exit(1)
<a name="l00079"></a>00079         
<a name="l00080"></a>00080     <span class="comment"># changing the resolution</span>
<a name="l00081"></a>00081     cmd = <span class="stringliteral">&#39;gdalwarp -r bilinear -tr 1000 1000 &#39;</span> + Temp2 + <span class="stringliteral">&#39; &#39;</span> +Ofile
<a name="l00082"></a>00082     <span class="keywordflow">try</span>:
<a name="l00083"></a>00083         returncode = call(cmd, shell=<span class="keyword">True</span>)
<a name="l00084"></a>00084         <span class="keywordflow">if</span> returncode:
<a name="l00085"></a>00085             <span class="keywordflow">print</span> <span class="stringliteral">&#39;Failure with returncode&#39;</span>, returncode; sys.exit(1)
<a name="l00086"></a>00086     <span class="keywordflow">except</span> OSError, message:
<a name="l00087"></a>00087         <span class="keywordflow">print</span> <span class="stringliteral">&#39;Execution failed!\n&#39;</span>, message; sys.exit(1)
<a name="l00088"></a>00088         
<a name="l00089"></a>00089     <span class="comment"># remove the temporary file</span>
<a name="l00090"></a>00090     <span class="keywordflow">try</span>:
<a name="l00091"></a>00091         os.remove(Temp)
<a name="l00092"></a>00092         os.remove(Temp2)
<a name="l00093"></a>00093     <span class="keywordflow">except</span>:
<a name="l00094"></a>00094         <span class="keywordflow">print</span> <span class="stringliteral">&#39;temp file not created&#39;</span>
<a name="l00095"></a>00095 
<a name="l00096"></a><a class="code" href="namespaceambhas_1_1gis.html#ade2cb2222bfcff3d88c38b0198429cd0">00096</a> <span class="keyword">def </span><a class="code" href="namespaceambhas_1_1gis.html#ade2cb2222bfcff3d88c38b0198429cd0">berambadi</a>(Ifile,Ofile):
<a name="l00097"></a>00097     <span class="comment"># define the file names file</span>
<a name="l00098"></a>00098     Temp1 = <span class="stringliteral">&#39;/home/tomer/MODISdata/temp/temp1.tif&#39;</span>
<a name="l00099"></a>00099     Temp2 = <span class="stringliteral">&#39;/home/tomer/MODISdata/temp/temp2.tif&#39;</span>
<a name="l00100"></a>00100             
<a name="l00101"></a>00101     <span class="comment"># convert from geographical co-ordinates to UTM zone 43</span>
<a name="l00102"></a>00102     cmd= <span class="stringliteral">&#39;gdalwarp -r bilinear -t_srs  \&#39;+proj=utm +zone=43 +datum=WGS84\&#39; &#39;</span> + Ifile + <span class="stringliteral">&#39; &#39;</span> +Temp1
<a name="l00103"></a>00103     <span class="keywordflow">try</span>:
<a name="l00104"></a>00104         returncode = call(cmd, shell=<span class="keyword">True</span>)
<a name="l00105"></a>00105         <span class="keywordflow">if</span> returncode:
<a name="l00106"></a>00106             <span class="keywordflow">print</span> <span class="stringliteral">&#39;Failure with returncode&#39;</span>, returncode; sys.exit(1)
<a name="l00107"></a>00107     <span class="keywordflow">except</span> OSError, message:
<a name="l00108"></a>00108         <span class="keywordflow">print</span> <span class="stringliteral">&#39;Execution failed!\n&#39;</span>, message; sys.exit(1)
<a name="l00109"></a>00109 
<a name="l00110"></a>00110     <span class="comment"># cut the area around Kabini Basin (output image will be of size 129 X 102)</span>
<a name="l00111"></a>00111     bmd=<span class="stringliteral">&#39;664000 1309000 685000 1294000&#39;</span>
<a name="l00112"></a>00112     cmd = <span class="stringliteral">&#39;gdal_translate -a_ullr &#39;</span> + bmd + <span class="stringliteral">&#39; -projwin &#39;</span> + bmd + <span class="stringliteral">&#39; &#39;</span> + Temp1+ <span class="stringliteral">&#39; &#39;</span> + Temp2
<a name="l00113"></a>00113     <span class="keywordflow">try</span>:
<a name="l00114"></a>00114         returncode = call(cmd, shell=<span class="keyword">True</span>)
<a name="l00115"></a>00115         <span class="keywordflow">if</span> returncode:
<a name="l00116"></a>00116             <span class="keywordflow">print</span> <span class="stringliteral">&#39;Failure with returncode&#39;</span>, returncode; sys.exit(1)
<a name="l00117"></a>00117     <span class="keywordflow">except</span> OSError, message:
<a name="l00118"></a>00118         <span class="keywordflow">print</span> <span class="stringliteral">&#39;Execution failed!\n&#39;</span>, message; sys.exit(1)
<a name="l00119"></a>00119         
<a name="l00120"></a>00120     <span class="comment"># changing the resolution</span>
<a name="l00121"></a>00121     cmd = <span class="stringliteral">&#39;gdalwarp -r bilinear -tr 1000 1000 &#39;</span> + Temp2 + <span class="stringliteral">&#39; &#39;</span> +Ofile
<a name="l00122"></a>00122     <span class="keywordflow">try</span>:
<a name="l00123"></a>00123         returncode = call(cmd, shell=<span class="keyword">True</span>)
<a name="l00124"></a>00124         <span class="keywordflow">if</span> returncode:
<a name="l00125"></a>00125             <span class="keywordflow">print</span> <span class="stringliteral">&#39;Failure with returncode&#39;</span>, returncode; sys.exit(1)
<a name="l00126"></a>00126     <span class="keywordflow">except</span> OSError, message:
<a name="l00127"></a>00127         <span class="keywordflow">print</span> <span class="stringliteral">&#39;Execution failed!\n&#39;</span>, message; sys.exit(1)
<a name="l00128"></a>00128         
<a name="l00129"></a>00129     <span class="comment"># remove the temporary file</span>
<a name="l00130"></a>00130     <span class="keywordflow">try</span>:
<a name="l00131"></a>00131         os.remove(Temp1)
<a name="l00132"></a>00132         os.remove(Temp2)
<a name="l00133"></a>00133     <span class="keywordflow">except</span>:
<a name="l00134"></a>00134         <span class="keywordflow">print</span> <span class="stringliteral">&#39;temp file not created&#39;</span>
<a name="l00135"></a>00135 
</pre></div></div><!-- contents -->
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="gis_8py.html">gis.py</a>      </li>

    <li class="footer">Generated on Sat Jul 21 2012 12:26:08 for AMBHAS by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.6.1 </li>
   </ul>
 </div>


</body>
</html>
